package com.csdj.udp;

import java.io.IOException;
import java.net.*;

public class UDPClient {
    public static void main(String[] args) {
        try {
            //创建客户端的socket对象
            DatagramSocket datagramSocket = new DatagramSocket();
            String conent = "你好！我是基于UDP协议发送的信息";
            InetAddress inetAddress = InetAddress.getByName("127.0.0.1");
            //封装数据包
            //1,发送数据 2.发送数据长度， 3.ip地址 4,端口号
            DatagramPacket datagramPacket  = new DatagramPacket(conent.getBytes(),
                        conent.getBytes().length,inetAddress,12345);
            //发送
            datagramSocket.send(datagramPacket);
            datagramSocket.close(); //关闭资源
        } catch (SocketException e) {
            e.printStackTrace();
        } catch (UnknownHostException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
